package com.example.studentsRoom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studentsRoom.entity.Dormitory;
import com.example.studentsRoom.entity.Moveout;
import com.example.studentsRoom.entity.Student;
import com.example.studentsRoom.form.SearchForm;
import com.example.studentsRoom.mapper.DormitoryMapper;
import com.example.studentsRoom.mapper.MoveoutMapper;
import com.example.studentsRoom.mapper.StudentMapper;
import com.example.studentsRoom.service.IMoveoutService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.studentsRoom.util.CommonUtil;
import com.example.studentsRoom.vo.MoveOutVo;
import com.example.studentsRoom.vo.PageVo;
import com.example.studentsRoom.vo.TransitionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-07-19
 */
@Service
public class MoveoutService extends ServiceImpl<MoveoutMapper, Moveout> implements IMoveoutService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private DormitoryMapper dormitoryMapper;

    @Autowired
    private MoveoutMapper moveoutMapper;
    /**
     * 分压查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageVo listPage(Integer page, Integer size) {
        Page<Student> studentPage=new Page<>(page,size);

        QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("stu_state","入住");
        Page<Student> resultPage = this.studentMapper.selectPage(studentPage, queryWrapper);

        PageVo pageVo = TransitionVo.transition(resultPage, dormitoryMapper);
        return pageVo;
    }

    /**
     * 模糊查询+分页
     * @param searchForm
     * @return
     */
    @Override
    public PageVo search(SearchForm searchForm) {
        //模糊查询加分页
        Page<Student> studentPage=new Page<>(searchForm.getPage(),searchForm.getSize());
        Page<Student> resultPage = null;

        QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("stu_state","入住");
        //判断value值是不是null
        if (searchForm.getValue().equals("")){
            resultPage = this.studentMapper.selectPage(studentPage, queryWrapper);
        }else{
            QueryWrapper<Student> wrapper=new QueryWrapper<>();
            wrapper.like(searchForm.getKey(),searchForm.getValue()).like("stu_state","入住");
            resultPage = this.studentMapper.selectPage(studentPage, wrapper);
        }

        PageVo pageVo = TransitionVo.transition(resultPage, dormitoryMapper);
        return pageVo;
    }

    @Override
    public Boolean moveout(String id, String value) {
        Moveout moveout=new Moveout();
        moveout.setStuId(id);

        Student student = studentMapper.selectById(id);
        moveout.setDorId(student.getDorId());

        moveout.setMSeason(value);
        moveout.setMDate(CommonUtil.creatDate());

        int insert = moveoutMapper.insert(moveout);
        if (insert!=1) return false;

        student.setStuState(value);
        int i = studentMapper.updateById(student);
        if (i!=1) return false;

        try{
            dormitoryMapper.addAvailable(student.getDorId());
        }catch (Exception e){
            return false;
        }

        return true;
    }

    @Override
    public PageVo moveList(Integer page, Integer size) {
        Page<Moveout> moveoutPage=new Page<>(page,size);
        Page<Moveout> resultPage = moveoutMapper.selectPage(moveoutPage, null);

        PageVo pageVo=new PageVo();
        pageVo.setTotal(resultPage.getTotal());

        List<MoveOutVo> moveOutVos=new ArrayList<>();

        for (Moveout record : resultPage.getRecords()) {
            MoveOutVo moveOutVo=new MoveOutVo();
            BeanUtils.copyProperties(record,moveOutVo);

            Student student = studentMapper.selectById(record.getStuId());
            Dormitory dormitory = dormitoryMapper.selectById(record.getDorId());
            moveOutVo.setDorName(dormitory.getDorName());
            moveOutVo.setStuName(student.getStuName());
            moveOutVo.setStuId(student.getStuId());

            moveOutVos.add(moveOutVo);
        }

        pageVo.setData(moveOutVos);

        return pageVo;
    }

    /**
     * 模糊查询+分页
     * @param searchForm
     * @return
     */
    @Override
    public PageVo moveSearch(SearchForm searchForm) {
        //模糊查询加分页
        Page<Moveout> moveoutPage=new Page<>(searchForm.getPage(),searchForm.getSize());
        Page<Moveout> resultPage = null;

        //判断value值是不是null
        if (searchForm.getValue().equals("")){
            resultPage = this.moveoutMapper.selectPage(moveoutPage, null);
        }else{
            QueryWrapper<Moveout> wrapper=new QueryWrapper<>();
            wrapper.like(searchForm.getKey(),searchForm.getValue());
            resultPage = this.moveoutMapper.selectPage(moveoutPage, wrapper);
        }

        PageVo pageVo=new PageVo();
        pageVo.setTotal(resultPage.getTotal());

        List<MoveOutVo> moveOutVos=new ArrayList<>();

        for (Moveout record : resultPage.getRecords()) {
            MoveOutVo moveOutVo=new MoveOutVo();
            BeanUtils.copyProperties(record,moveOutVo);

            Student student = studentMapper.selectById(record.getStuId());
            Dormitory dormitory = dormitoryMapper.selectById(record.getDorId());
            moveOutVo.setDorName(dormitory.getDorName());
            moveOutVo.setStuName(student.getStuName());
            moveOutVo.setStuId(student.getStuId());

            moveOutVos.add(moveOutVo);
        }

        pageVo.setData(moveOutVos);
        return pageVo;
    }
}
